package com.jingyanku.struts2;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.base.Pagination.Pagination;
import com.jingyanku.pojo.Board;
import com.jingyanku.pojo.Category;
import com.jingyanku.pojo.Reply;
import com.jingyanku.pojo.Thread;
import com.jingyanku.service.IBoardService;
import com.jingyanku.service.IReplyService;
import com.jingyanku.service.IThreadService;
import com.opensymphony.xwork2.ActionSupport;
import com.system.pojo.Person;
import com.system.service.IPersonService;
import com.system.struts2.util.PersonInfo;
import com.system.struts2.util.PersonUtil;

public class ThreadAction extends ActionSupport {
	private Thread thread;

	private Person person;

	private Board board;

	private Reply reply;

	private Category category;

	private IThreadService<Thread> threadSer;

	private IPersonService<Person> personSer;

	private IBoardService<Board> boardSer;

	private IReplyService<Reply> replySer;

	public List<Thread> threadList = new ArrayList<Thread>();
	public List<Reply> replyList = new ArrayList<Reply>();

	private Pagination pagination;

	HttpServletRequest request = null;
	HttpServletResponse response = null;
	private String flag;// 是否可编辑帖子

	public String list() {

		return this.view();
	}

	public String view() {

		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();

		thread = threadSer.find(Thread.class, thread.getId());

		if (person == null) {

			int totalCount = replySer.getTotalCount(
					" select count(r) from Reply r "
							+ " where r.deleted = false and r.thread.id = "
							+ thread.getId(), null);

			pagination = new Pagination(request, response);
			pagination.setRecordCount(totalCount);

			replyList = replySer.list(
					" from Reply r where r.deleted = false and r.thread.id = "
							+ thread.getId(), pagination.getFirstResult(),
					pagination.getPageSize(), null);
			flag ="hit";
		} else {
			int totalCount = replySer.getTotalCount(
					" select count(r) from Reply r "
							+ " where r.deleted = false and r.thread.id = "
							+ thread.getId() + " and r.author ="
							+ person.getId(), null);

			pagination = new Pagination(request, response);
			pagination.setRecordCount(totalCount);

			replyList = replySer.list(
					" from Reply r where r.deleted = false and r.thread.id = "
							+ thread.getId() + " and r.author ="
							+ person.getId(), pagination.getFirstResult(),
					pagination.getPageSize(), null);
		}

		if (flag != null && flag.equals("hit"))// 点击进入人气增加1，进入之后操作人气不增加！
			threadSer.updateHit(thread.getId());
		category = thread.getBoard().getCategory();
		board = thread.getBoard();

		if (flag != null && flag.equals("edit"))
			return "modify";
		else
			return "list";// new ActionForward("list",
							// "/form/thread/viewThread.jsp", false);
	}

	public String initAdd() {

		board = boardSer.find(Board.class, board.getId());

		return "initAdd";// new ActionForward("add",
							// "/form/thread/addThread.jsp", false);
	}

	public String add() {
		board = boardSer.find(Board.class, board.getId());
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
		person = personSer.find(Person.class, personInfo.getId());
		thread.setBoard(board);
		thread.setAuthor(person);
		thread.setAuthorLastReplied(null);
		thread.setDateCreated(new Date());
		thread.setDateLastReplied(new Date());
		thread.setDeleted(false);
		thread.setIpCreated(request.getRemoteAddr());
		thread.setReadonly(false);
		thread.setReplyCount(0);
		thread.setTopped(false);
		thread.setVersion("1");
		thread.setHit(0);

		threadSer.create(thread);

		return this.list();
	}

	public String modify() {
		Thread threadFd = threadSer.find(Thread.class, thread.getId());
		threadFd.setContent(thread.getContent());
		threadSer.save(threadFd);
		return this.list();
	}

	public String delete() {
		thread = threadSer.find(Thread.class, thread.getId());
		thread.getBoard()
				.setThreadCount(thread.getBoard().getThreadCount() - 1);
		thread.getBoard().setReplyCount(
				thread.getBoard().getReplyCount() - thread.getReplyCount());
		thread.setDeleted(true);
		boardSer.save(thread.getBoard());
		threadSer.save(thread);
		// thread.
		request = ServletActionContext.getRequest();
		request.setAttribute("board", thread.getBoard());
		return "delete";
	}

	public Thread getThread() {
		return thread;
	}

	public void setThread(Thread thread) {
		this.thread = thread;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public IThreadService<Thread> getThreadSer() {
		return threadSer;
	}

	public void setThreadSer(IThreadService<Thread> threadSer) {
		this.threadSer = threadSer;
	}

	public IPersonService<Person> getPersonSer() {
		return personSer;
	}

	public void setPersonSer(IPersonService<Person> personSer) {
		this.personSer = personSer;
	}

	public IBoardService<Board> getBoardSer() {
		return boardSer;
	}

	public void setBoardSer(IBoardService<Board> boardSer) {
		this.boardSer = boardSer;
	}

	public IReplyService<Reply> getReplySer() {
		return replySer;
	}

	public void setReplySer(IReplyService<Reply> replySer) {
		this.replySer = replySer;
	}

	public List<Reply> getReplyList() {
		return replyList;
	}

	public void setReplyList(List<Reply> replyList) {
		this.replyList = replyList;
	}

	public List<Thread> getThreadList() {
		return threadList;
	}

	public void setThreadList(List<Thread> threadList) {
		this.threadList = threadList;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

}
